﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.IO;
using System.Web;
using System.Text.RegularExpressions;

namespace V5_Common {
    /// <summary>
    /// Html帮助类
    /// </summary>
    public class HtmlHelper {
        private static readonly HtmlHelper m_Instance = new HtmlHelper();
        /// <summary>
        /// 
        /// </summary>
        public static HtmlHelper Instance {
            get { return m_Instance; }
        }
        /// <summary>
        /// 
        /// </summary>
        public HtmlHelper() {

        }
        /// <summary>
        /// 加载用户控件
        /// </summary>
        /// <param name="pathName"></param>
        /// <returns></returns>
        public string Include(string pathName) {
            try {
                /*
                 public static string RangerUsControl(string controlName)
        {
            StringBuilder build = new StringBuilder();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(new StringWriter(build));
            UserControl uc = new UserControl();
            Control ctrl=uc.LoadControl(controlName+".ascx");//加载用户定义控件
           
            TextBox txtBox1 = ctrl.FindControl("TextBox1") as TextBox;//获得id为“TextBox1”的控件
            txtBox1.Text = "测试"; //给控件初始化
            string result;
            try 
            { 
                ctrl.RenderControl(htmlWriter);
            }
            catch { }
            finally 
            { 
                htmlWriter.Flush();
                result=build.ToString();
            }

            return result;//返回控件的HTML代码
        }
                 */
                System.Web.UI.Page page = new System.Web.UI.Page();
                UserControl ctl = (UserControl)page.LoadControl(pathName);
                page.Controls.Add(ctl);
                StringWriter writer = new StringWriter();
                HttpContext.Current.Server.Execute(page, writer, true);
                return writer.ToString();
            }
            catch (Exception ex) { return pathName + "加载失败!" + ex.Message + ex.InnerException; }
        }

        /// <summary>
        /// 返回指定字符串的值
        /// </summary>
        /// <param name="Tag"></param>
        /// <param name="TagName"></param>
        /// <returns></returns>
        public string TagVal(string Tag, string TagName) {
            string[] strArray = Tag.Split(new string[] { "," }, StringSplitOptions.None);
            for (int i = 0; i < strArray.Length; i++) {
                Regex regex = new Regex(@"(?<Keyword>\w+)\s*=\s*(?<Value>.*)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                for (Match match = regex.Match(strArray[i]); match.Success; match = match.NextMatch()) {
                    if (match.Groups["Keyword"].ToString().ToLower().IndexOf(TagName.ToLower()) != -1) {
                        return match.Groups["Value"].ToString().ToLower();
                    }
                }
            }
            return "";
        }

        /// <summary>
        /// Html转换成js
        /// </summary>
        /// <param name="oTxt"></param>
        /// <returns></returns>
        public string HtmlToJS(string oTxt) {
            oTxt = "document.writeln(\"" + oTxt.Replace(@"\", @"\\").Replace("/", @"\/").Replace("'", @"\'").Replace("\"", "\\\"") + "\");";
            return oTxt;
        }


        #region UBB代码
        public static string UBB2HTML(string Str) {
            string OutPut = Str;
            Match match1 = new Regex(@"(\[b\])([ \S\t]*?)(\[\/b\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<B>" + match1.Groups[2].ToString() + "</B>");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"(\[i\])([ \S\t]*?)(\[\/i\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<I>" + match1.Groups[2].ToString() + "</I>");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"(\[U\])([ \S\t]*?)(\[\/U\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<U>" + match1.Groups[2].ToString() + "</U>");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"((\r\n)*\[p\])(.*?)((\r\n)*\[\/p\])", RegexOptions.Singleline | RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<P class=\"pstyle\">" + match1.Groups[3].ToString() + "</P>");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"(\[sup\])([ \S\t]*?)(\[\/sup\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<SUP>" + match1.Groups[2].ToString() + "</SUP>");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"(\[sub\])([ \S\t]*?)(\[\/sub\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<SUB>" + match1.Groups[2].ToString() + "</SUB>");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"(\[url\])([ \S\t]*?)(\[\/url\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<A href=\"" + match1.Groups[2].ToString() + "\" target=\"_blank\">" + match1.Groups[2].ToString() + "</A>");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"(\[url=([ \S\t]+)\])([ \S\t]*?)(\[\/url\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<A href=\"" + match1.Groups[2].ToString() + "\" target=\"_blank\">" + match1.Groups[3].ToString() + "</A>");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"(\[email\])([ \S\t]*?)(\[\/email\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<A href=\"mailto:" + match1.Groups[2].ToString() + "\" target=\"_blank\">" + match1.Groups[2].ToString() + "</A>");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"(\[email=([ \S\t]+)\])([ \S\t]*?)(\[\/email\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<A href=\"mailto:" + match1.Groups[2].ToString() + "\" target=\"_blank\">" + match1.Groups[3].ToString() + "</A>");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"(\[size=([1-7])\])([ \S\t]*?)(\[\/size\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<FONT SIZE=" + match1.Groups[2].ToString() + ">" + match1.Groups[3].ToString() + "</FONT>");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"(\[color=([\S]+)\])([ \S\t]*?)(\[\/color\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<FONT COLOR=" + match1.Groups[2].ToString() + ">" + match1.Groups[3].ToString() + "</FONT>");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"(\[font=([\S]+)\])([ \S\t]*?)(\[\/font\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<FONT FACE=" + match1.Groups[2].ToString() + ">" + match1.Groups[3].ToString() + "</FONT>");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"\[picture\](\d+?)\[\/picture\]", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<A href=\"ShowImage.aspx?Type=ALL&Action=forumImage&ImageID=" + match1.Groups[1].ToString() + "\" target=\"_blank\"><IMG border=0 Title=\u70b9\u51fb\u6253\u5f00\u65b0\u7a97\u53e3\u67e5\u770b src=\"ShowImage.aspx?Action=forumImage&ImageID=" + match1.Groups[1].ToString() + "\"></A>");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"(\[align=([\S]+)\])([ \S\t]*?)(\[\/align\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<P align=" + match1.Groups[2].ToString() + ">" + match1.Groups[3].ToString() + "</P>");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"(\[H=([1-6])\])([ \S\t]*?)(\[\/H\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<H" + match1.Groups[2].ToString() + ">" + match1.Groups[3].ToString() + "</H" + match1.Groups[2].ToString() + ">");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"(\[list(=(A|a|I|i| ))?\]([ \S\t]*)\r\n)((\[\*\]([ \S\t]*\r\n))*?)(\[\/list\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                string text2 = match1.Groups[5].ToString();
                Regex regex2 = new Regex(@"\[\*\]([ \S\t]*\r\n?)", RegexOptions.IgnoreCase);
                for (Match match2 = regex2.Match(text2); match2.Success; match2 = match2.NextMatch()) {
                    text2 = text2.Replace(match2.Groups[0].ToString(), "<LI>" + match2.Groups[1]);
                }
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<UL TYPE=\"" + match1.Groups[3].ToString() + "\"><B>" + match1.Groups[4].ToString() + "</B>" + text2 + "</UL>");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"\[shadow=(?<x>[^\]]*),(?<y>[^\]]*),(?<z>[^\]]*)\](?<w>[^\]]*)\[/shadow\]", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), @"<table width=" + match1.Groups[1] + " style=\"filter:shadow(color=" + match1.Groups[2] + ", strength=" + match1.Groups[3] + ")\">" + match1.Groups[4] + "</table>");

                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"(\[glow=)(\d*?),(#*\w*?),(\d*?)\]([\S\t]*?)(\[\/glow\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<TABLE WIDTH=" + match1.Groups[2].ToString() + "  STYLE=FILTER:GLOW(COLOR=" + match1.Groups[3].ToString() + ", STRENGTH=" + match1.Groups[4].ToString() + ")>" + match1.Groups[5].ToString() + "</TABLE>");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"(\[center\])([ \S\t]*?)(\[\/center\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<CENTER>" + match1.Groups[2].ToString() + "</CENTER>");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"(\[IMG\])(http|https|ftp):\/\/([ \S\t]*?)(\[\/IMG\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<br><a onfocus=this.blur() href=" + match1.Groups[2].ToString() + "://" + match1.Groups[3].ToString() + " target=_blank><IMG SRC=" + match1.Groups[2].ToString() + "://" + match1.Groups[3].ToString() + " border=0 onload=javascript:if(screen.width-333<this.width)this.width=screen.width-333></a>");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"(\[em([\S\t]*?)\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<img src=Images/ubb/face/" + match1.Groups[2].ToString() + ".gif border=0 align=middle>");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"(\[img\])([ \S\t]*?)(\[\/img\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<a target=_blank href=" + match1.Groups[2].ToString() + "><IMG SRC=" + match1.Groups[2].ToString() + " border=0 onload='javascript:if(screen.width-333<this.width)this.width=screen.width-333'></a>");
                match1 = match1.NextMatch();
            }

            match1 = new Regex(@"(\[flash=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/flash\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<a href=" + match1.Groups[4].ToString() + " TARGET=_blank><IMG SRC=images/swf.gif border=0> [\u5168\u5c4f\u6b23\u8d4f]</a><br><br><OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=" + match1.Groups[2].ToString() + " height=" + match1.Groups[3].ToString() + "><PARAM NAME=movie VALUE=" + match1.Groups[4].ToString() + "><PARAM NAME=quality VALUE=high><param name=menu value=false><embed src=" + match1.Groups[4].ToString() + " quality=high menu=false pluginspage=http://www.macromedia.com/go/getflashplayer type=application/x-shockwave-flash width=" + match1.Groups[2].ToString() + " height=" + match1.Groups[3].ToString() + ">" + match1.Groups[4].ToString() + "</embed></OBJECT>");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"(\[dir=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/dir\])", RegexOptions.IgnoreCase).Match(Str);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<object classid=clsid:166B1BCA-3F9C-11CF-8075-444553540000 codebase=http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=7,0,2,0 width=" + match1.Groups[2].ToString() + " height=" + match1.Groups[3].ToString() + "><param name=src value=" + match1.Groups[4].ToString() + "><embed src=" + match1.Groups[4].ToString() + " pluginspage=http://www.macromedia.com/shockwave/download/ width=" + match1.Groups[2].ToString() + " height=" + match1.Groups[3].ToString() + "></embed></object>");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"(\[rm=)(\d*?),(\d*?),([\S\t]*?)\]([\S\t]*?)(\[\/rm\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<OBJECT classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA class=OBJECT id=RAOCX width=" + match1.Groups[2].ToString() + " height=" + match1.Groups[3].ToString() + "><PARAM NAME=SRC VALUE=" + match1.Groups[5].ToString() + "><PARAM NAME=CONSOLE VALUE=Clip1><PARAM NAME=CONTROLS VALUE=imagewindow><PARAM NAME=AUTOSTART VALUE=" + match1.Groups[4].ToString() + "></OBJECT><br><OBJECT classid=CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA height=32 id=video2 width=" + match1.Groups[2].ToString() + "><PARAM NAME=SRC VALUE=" + match1.Groups[5].ToString() + "><PARAM NAME=AUTOSTART VALUE=" + match1.Groups[4].ToString() + "><PARAM NAME=CONTROLS VALUE=controlpanel><PARAM NAME=CONSOLE VALUE=Clip1></OBJECT>");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"(\[mp=)(\d*?),(\d*?),([\S\t]*?)\]([\S\t]*?)(\[\/mp\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<object align=middle classid=CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95 class=OBJECT id=MediaPlayer width=" + match1.Groups[2].ToString() + " height=" + match1.Groups[3].ToString() + " ><PARAM NAME=AUTOSTART VALUE=" + match1.Groups[4].ToString() + "><param name=Filename value=" + match1.Groups[5].ToString() + "><PARAM NAME=showstatusbar VALUE=1><embed type=application/x-oleobject codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename=mp src=" + match1.Groups[5].ToString() + "  width=" + match1.Groups[2].ToString() + " height=" + match1.Groups[3].ToString() + " showstatusbar=1></embed></object>");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"(\[qt=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/qt\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<embed src=" + match1.Groups[4].ToString() + " width=" + match1.Groups[2].ToString() + " height=" + match1.Groups[3].ToString() + " autoplay=true loop=false controller=true playeveryframe=false cache=false scale=TOFIT bgcolor=#000000 kioskmode=false targetcache=false pluginspage=http://www.apple.com/quicktime/>");
                match1 = match1.NextMatch();
            }

            match1 = new Regex(@"(\[QUOTE\])([ \S\t]*?)(\[\/QUOTE\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "以下内容为引用：<table border=0 width=95% cellpadding=10 cellspacing=1 bgcolor=#000000><tr><td bgcolor=#FFFFFF>" + match1.Groups[2].ToString() + "</td></tr></table>");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"(\[move\])([ \S\t]*?)(\[\/move\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<MARQUEE scrollamount=3>" + match1.Groups[2].ToString() + "</MARQUEE>");
                match1 = match1.NextMatch();
            }
            match1 = new Regex(@"(\[FLY\])([ \S\t]*?)(\[\/FLY\])", RegexOptions.IgnoreCase).Match(OutPut);
            while (match1.Success) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<MARQUEE width=80% behavior=alternate scrollamount=3>" + match1.Groups[2].ToString() + "</MARQUEE>");
                match1 = match1.NextMatch();
            }
            Regex regex1 = new Regex(@"(\[image\])([ \S\t]*?)(\[\/image\])", RegexOptions.IgnoreCase);
            for (match1 = regex1.Match(OutPut); match1.Success; match1 = match1.NextMatch()) {
                OutPut = OutPut.Replace(match1.Groups[0].ToString(), "<img src=\"" + match1.Groups[2].ToString() + "\" border=0 align=middle><br>");
            }
            return OutPut;
        }
        #endregion

        /// <summary>
        /// 过滤所有Html标签
        /// </summary>
        /// <param name="HTMLStr"></param>
        /// <returns></returns>
        public string ParseTags(string HTMLStr) {
            return System.Text.RegularExpressions.Regex.Replace(HTMLStr, "<[^>]*>", "");
        }

    }
}
